package Leetcode.ArrayAndLinkedList.twosum1;

import java.util.HashMap;
import java.util.Map;

/**
 * 该解法用到了 哈希表
 */
public class Solution2 {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        if(len == 0) {
            return null;
        }

        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < len; i++) {
            map.put(nums[i],i);
        }
        for(int i = 0; i < len; i++) {
            int num = target - nums[i];
            if(map.containsKey(num) && map.get(num) != i) {
                return new int[] {i,map.get(num)};
            }
        }
        throw new IllegalArgumentException("未找到");
    }

    public static void main(String[] args) {
        int[] nums = {3,3};
        Solution2 solution2 = new Solution2();
        int[] result = solution2.twoSum(nums, 6);
        for(int i : result) {
            System.out.println(i);
        }

    }
}